package com.crm.user.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.base.Strings;
import com.crm.base.controller.BaseController;
import com.crm.customers.model.Contract;
import com.crm.customers.model.ContractMoneylog;
import com.crm.customers.service.ContractMoneylogService;
import com.crm.customers.service.ContractService;
import com.crm.util.Pager;

/**
 * 财务管理controller
 * */
@Controller
public class FinanceController extends BaseController {
	
	@Autowired
	private ContractService contractService;
	
	@Autowired
	private ContractMoneylogService contractMoneylogService;
	
 
	
	/**
	 * 收款URL拼接
	 * @throws Exception 
	 * */
	/*@RequestMapping(value="/finance/receivableslisturl",produces="text/html;charset=UTF-8")
	public String receivableslisturl(@ModelAttribute ContractMoneylog c,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		String url = "/customer/intoreceivableslist";
		url += "p"+getSkipToPage();
		if (c.getType() != null) {
			url += "_t"+c.getType();
		}
		if (c.getState() != null) {
			url += "_s"+c.getState();
		}
		if (!Strings.isNullOrEmpty(c.getKeyWord())){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(c.getKeyWord(), "UTF-8"),"UTF-8");
		}
		return "redirect:" + url;
	}*/
	

	/**
	 * 付款URL拼接
	 * @throws Exception 
	 * */
	@RequestMapping(value="/finance/paymentlisturl",produces="text/html;charset=UTF-8")
	public String paymentlisturl(@ModelAttribute ContractMoneylog c,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		String url = "/finance/paymentlist";
		url += "p"+getSkipToPage();
		if (c.getType() != null) {
			url += "_t"+c.getType();
		}
		if (c.getState() != null) {
			url += "_s"+c.getState();
		}
		if (c.getArrivaltime() != null && c.getArrivaltime().length() > 0) {
			url += "_a"+c.getArrivaltime();
		}
		if (!Strings.isNullOrEmpty(c.getKeyWord())){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(c.getKeyWord(), "UTF-8"),"UTF-8");
		}
		
		return "redirect:" + url;
	}
	 
	
	/**
	 * 进入收款列表页面
	 * @throws UnsupportedEncodingException 
	 * */
	@RequestMapping(value="/finance/paymentlist{k}", produces="text/html;charset=UTF-8")
	public String intoReceivablesList(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		String url = "financemanage/paymentlist";
		Integer pageNo = 1;
		Integer pageSize = 10;
		String keyWord = null;
		
		ContractMoneylog c = new ContractMoneylog();
		
		if (k != null) {
			String key[] = k.split("_");
			for (String kk : key) {
				if (kk.indexOf("p") > -1) {
					pageNo = Integer.parseInt(kk.replace("p", ""));
				} else if (kk.indexOf("t") > -1) {
					c.setType(Integer.parseInt(kk.replace("t", "")));
					if(c.getType()==1){
						url = "financemanage/receivableslist" ;
					}
				} else if (kk.indexOf("a") > -1) {
					c.setArrivaltime(kk.replace("a", ""));
				}
			}
		}
		
		if(request.getParameter("kwd")!=null){  
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
			c.setKeyWord(keyWord);
		}
		List<ContractMoneylog>  list = contractMoneylogService.getContractMoneylogList(c, pageNo, pageSize);
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/finance/paymentlist"+k);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("KeyWord",keyWord);
		params.put("state", c.getState());
		params.put("type", c.getType());
		params.put("arrivaltime", c.getArrivaltime());
		pager.setParams(params);
		request.setAttribute("pager", pager); 
		request.setAttribute("type", c.getType());
		return url;	
	}
	
	/**
	 * 垫款URL拼接
	 * @throws Exception 
	 * */
	@RequestMapping(value="/finance/advancelisturl",produces="text/html;charset=UTF-8")
	public String advancelisturl(@ModelAttribute ContractMoneylog c,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		String url = "/finance/advancelist";
		url += "p"+getSkipToPage();
		if (c.getState() != null && c.getState() != -1) {
			url += "_s"+c.getState();
		}
		if (c.getApprovalstatus() != null && c.getApprovalstatus() != -1) {
			url += "_a"+c.getApprovalstatus();
		}
		if (c.getContractsign() != null && c.getContractsign() != -1) {
			url += "_c"+c.getContractsign();
		}
		if (c.getSttime() != null) {
			url += "_ti"+c.getSttime();
		}
		if (c.getEntime() != null) {
			url += "_et" + c.getEntime();
		}
		if (!Strings.isNullOrEmpty(c.getKeyWord())){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(c.getKeyWord(), "UTF-8"),"UTF-8");
		}
		
		return "redirect:" + url;
	}
	 
	
	/**
	 * 进入垫款列表页面
	 * @throws UnsupportedEncodingException
	 * */
	@RequestMapping(value="/finance/advancelist{k}", produces="text/html;charset=UTF-8")
	public String intoAdvanceManagePage(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		String url = "/financemanage/advancelist";
		Integer pageNo = 1;
		Integer pageSize = 10;
		String keyWord = null;
		Integer contractsign = null;
		
		ContractMoneylog c = new ContractMoneylog();
		c.setType(0);
		c.setPaytype(0);//预付款
		//c.setCreateuser(getLoginUser().getId());
		if (k != null) {
			String key[] = k.split("_");
			for (String kk : key) {
				if (kk.indexOf("p") > -1) {
					pageNo = Integer.parseInt(kk.replace("p", ""));
				}  
				if (kk.indexOf("a") > -1) {
					c.setApprovalstatus(Integer.parseInt(kk.replace("a", "")));
				}
				if (kk.indexOf("s") > -1) {
					c.setState(Integer.parseInt(kk.replace("s", "")));
				}
				if (kk.indexOf("c") > -1) {
					c.setContractsign(Integer.parseInt(kk.replace("c", "")));
				}
				if (kk.indexOf("ti") > -1) {
					c.setSttime(kk.replace("ti", ""));
				}
				if (kk.indexOf("et") > -1) {
					c.setEntime(kk.replace("et", ""));
				}
			}
		}
		if(request.getParameter("kwd")!=null){  
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
			c.setKeyWord(keyWord);
		}
		List<ContractMoneylog>  list = contractMoneylogService.getContractMoneylogList(c, pageNo, pageSize);
		
		int totalmoney = contractMoneylogService.selectTotalMoney(c);
		 
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/finance/advancelist"+k);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("KeyWord",keyWord);
		params.put("state", c.getState());
		params.put("approvalstatus", c.getApprovalstatus());
		params.put("type", c.getType());
		params.put("arrivaltime", c.getArrivaltime());
		params.put("contractsign", c.getContractsign());
		params.put("sttime",c.getSttime());
		params.put("entime", c.getEntime());
		pager.setParams(params);
		request.setAttribute("totalmoney", totalmoney);
		request.setAttribute("pager", pager); 
		return url;	
	}
	
	
	  
	
	

}
